//
//  HudManager.m
//  LCProgressHUDemo
//
//  Created by 度周末网络-王腾 on 15/12/1.
//  Copyright © 2015年 Leo. All rights reserved.
//

#import "HudManager.h"

@implementation HudManager

+(instancetype)shareManager{
    static HudManager *hud;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        hud = [[HudManager alloc] init];
    });
    return hud;
}

-(id)init{
    [self initHud];
    return [super init];
}

-(void)initHud{
    
    self.bundlePath = [[NSBundle mainBundle] pathForResource:@"HudManager" ofType:@"bundle"];
    self.window    = [UIApplication sharedApplication].keyWindow;
    self.hudBgView = [[UIView alloc ] initWithFrame:CGRectMake(0, 64, self.window.frame.size.width, self.window.frame.size.height - 64*2)];
    [self.window addSubview:self.hudBgView];
    self.hud    = [[MBProgressHUD alloc ] initWithView:self.hudBgView];
}

/** 在 window 上添加一个 HUD */
- (void)showStatus:(HudManagerStatus)status text:(NSString *)text{
    
    [[HudManager shareManager].hudBgView removeFromSuperview];
    MBProgressHUD *hud = [HudManager shareManager].hud;
    [hud show:YES];
    [hud setLabelText:text];
    [hud setRemoveFromSuperViewOnHide:YES];
    [hud removeFromSuperview];
    [[HudManager shareManager].hudBgView addSubview:hud];
    [[HudManager shareManager].window addSubview:[HudManager shareManager].hudBgView];

    switch (status) {
        case HudManagerStatusText:{
            [hud setMode:MBProgressHUDModeText];
            [HudManager hide:YES afterDelay:1.5];
            break;
        }
        case HudManagerStatusSuccess: {
            NSString *sucPath = [self.bundlePath stringByAppendingPathComponent:@"hud_success@2x.png"];
            UIImage *sucImage = [UIImage imageWithContentsOfFile:sucPath];
            
            hud.mode = MBProgressHUDModeCustomView;
            UIImageView *sucView = [[UIImageView alloc] initWithImage:sucImage];
            hud.customView = sucView;
            [HudManager hide:YES afterDelay:1.2];
        }
            break;
            
        case HudManagerStatusError: {
            
            NSString *errPath = [self.bundlePath stringByAppendingPathComponent:@"hud_error@2x.png"];
            UIImage *errImage = [UIImage imageWithContentsOfFile:errPath];
            
            hud.mode = MBProgressHUDModeCustomView;
            UIImageView *errView = [[UIImageView alloc] initWithImage:errImage];
            hud.customView = errView;
            [HudManager hide:YES afterDelay:1.5];
        }
            break;
            
        case HudManagerStatusWaitting: {
            hud.mode = MBProgressHUDModeIndeterminate;
        }
            break;
            
        case HudManagerStatusInfo: {
            
            NSString *infoPath = [self.bundlePath stringByAppendingPathComponent:@"hud_info@2x.png"];
            UIImage *infoImage = [UIImage imageWithContentsOfFile:infoPath];
            
            hud.mode = MBProgressHUDModeCustomView;
            UIImageView *infoView = [[UIImageView alloc] initWithImage:infoImage];
            hud.customView = infoView;
            [HudManager hide:YES afterDelay:1.2];
        }
            break;
        
        default:
            break;
    }
}

#pragma mark - 建议使用的方法

/** 在 window 上添加一个只显示文字的 HUD */
+ (void)showMessage:(NSString *)text{
    [[HudManager shareManager] showStatus:HudManagerStatusText text:text];
}

/** 在 window 上添加一个提示`信息`的 HUD */
+ (void)showInfoMsg:(NSString *)text{
    [[HudManager shareManager] showStatus:HudManagerStatusInfo text:text];
}

/** 在 window 上添加一个提示`失败`的 HUD */
+ (void)showFailure:(NSString *)text{
    [[HudManager shareManager] showStatus:HudManagerStatusError text:text];
}

/** 在 window 上添加一个提示`成功`的 HUD */
+ (void)showSuccess:(NSString *)text{
    [[HudManager shareManager] showStatus:HudManagerStatusSuccess text:text];
}

/** 在 window 上添加一个提示`等待`的 HUD, 需要手动关闭 */
+ (void)showLoading:(NSString *)text{
    if (text) {
        [[HudManager shareManager] showStatus:HudManagerStatusWaitting text:text];
    }
}


+ (void)showLoading{
   
    [[HudManager shareManager] showStatus:HudManagerStatusWaitting text:nil];
   
}

+ (void)hide:(BOOL)animated afterDelay:(NSTimeInterval)delay{
    MBProgressHUD *hud = [HudManager shareManager].hud;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [[HudManager shareManager].hudBgView removeFromSuperview];
        
        [hud removeFromSuperview];
    });
    
    [hud hide:YES afterDelay:delay];
}

/** 手动隐藏 HUD */
+ (void)hide{
    [HudManager hide:YES afterDelay:0.1];
}

@end
